package com.bigzero.workflow.modules.process.controller;

import com.bigzero.workflow.common.dto.TodoCompleteDto;
import com.bigzero.workflow.common.model.R;
import com.bigzero.workflow.common.model.TableDataInfo;
import com.bigzero.workflow.common.model.TokenInfo;
import com.bigzero.workflow.common.utils.TokenUtil;
import com.bigzero.workflow.modules.process.query.TodoQuery;
import com.bigzero.workflow.modules.process.service.TodoService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 代办流程
 *
 * @author fanxinxiong
 * @date 2025-02-14 19:12
 **/
@RequestMapping("/process/todo")
@RestController
@Tag(name = "与我有关-代办流程")
public class TodoController {

    @Autowired
    private TodoService todoService;

    /**
     * 查看我代办的流程
     *
     * @param todoQuery 参数
     */
    @ApiOperationSupport(author = "fanxinxiong", order = 1)
    @Operation(summary = "我代办的流程分页列表")
    @GetMapping("list")
    public TableDataInfo list(TodoQuery todoQuery) {
        TokenInfo tokenInfo = TokenUtil.getTokenInfo();
        todoQuery.setUserId(tokenInfo.getId());
        todoQuery.setDeptId(tokenInfo.getDeptId());
        return todoService.queryPage(todoQuery);
    }

    /**
     * 获取节点表单
     * @param taskId 任务id
     * @return 表单数据
     */
    @ApiOperationSupport(author = "fanxinxiong", order = 2)
    @Operation(summary = "获取节点表单")
    @Parameters({
        @Parameter(name = "taskId", description = "任务id", required = true)
    })
    @GetMapping("getNodeForm/{taskId}")
    public R<Map<String, Object>> getNodeForm(@PathVariable String taskId){
        Map<String, Object> nodeForm = todoService.getNodeForm(taskId);
        return R.ok(nodeForm);
    }

    /**
     * 审批节点
     *
     * @param dto 参数
     */
    @ApiOperationSupport(author = "fanxinxiong", order = 3)
    @Operation(summary = "审批节点")
    @PostMapping("complete")
    public R<String> complete(@RequestBody TodoCompleteDto dto) {
        TokenInfo tokenInfo = TokenUtil.getTokenInfo();
        dto.setUserId(tokenInfo.getId());
        dto.setDeptId(tokenInfo.getDeptId());
        todoService.complete(dto);
        return R.ok();
    }
}
